package com.zy.imagineCommunity.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.imagineCommunity.entity.Page;
import com.zy.imagineCommunity.entity.SystemMessage;
import com.zy.imagineCommunity.entity.User;
import com.zy.imagineCommunity.service.SystemMessageService;
import com.zy.imagineCommunity.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class SystemMessageController {

    @Autowired
    SystemMessageService systemMessageService;

    @Autowired
    UserSerivce userSerivce;

    @GetMapping("/toSystemMessageManagement")
    public String toSystemMessageManagement() {
        return "/SystemMessageManagement";
    }

    @ResponseBody
    @PostMapping("/getSystemMessage")
    public JSONObject getSystemMessage(int page) {

        JSONObject jsonObject = new JSONObject();

        Page<SystemMessage> systemMessagePage = systemMessageService.querySystemMessageByPageNoAndPageSize(page, 10);

        jsonObject.put("code", 0);
        jsonObject.put("count", systemMessagePage.getPageCountTotal());
        jsonObject.put("data", systemMessagePage.getItems());

        return jsonObject;
    }

    @ResponseBody
    @PostMapping("/addSystemMessage")
    public void addSystemMessage(String title, String message) {

        Date data = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        systemMessageService.addSystemMessage(new SystemMessage(null, title, message, simpleDateFormat.format(data)));

        List<User> userList = userSerivce.queryUserList();

        for (User user : userList) {
            userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), user.getLanguageState(), user.getMessageNum() + 1));
        }

    }

    @GetMapping("/toSystemMessage")
    public String toSystemMessage() {
        return "/SystemMessage";
    }

    /**
     * 系统消息列表，需要userId来修改个人系统消息个数
     *
     * @param userId
     * @param pageNo
     * @return
     */
    @ResponseBody
    @PostMapping("/systemMessageList")
    public JSONObject systemMessageList(int userId, int pageNo) {

        JSONObject jsonObject = new JSONObject();

        User user = userSerivce.queryUserByUserId(userId);

        userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), user.getLanguageState(), 0));

        Page<SystemMessage> page = systemMessageService.querySystemMessageByPageNoAndPageSize(pageNo, 10);

        jsonObject.put("page", page);

        return jsonObject;
    }

    @GetMapping("/toSystemMessageDetails/{id}")
    public String toSystemMessageListDetails(@PathVariable("id") int id, Model model) {
        model.addAttribute("id", id);
        return "SystemMessageDetails";
    }

    @ResponseBody
    @PostMapping("/systemMessageDetails")
    public JSONObject systemMessageListDetails(int id) {

        JSONObject jsonObject = new JSONObject();

        SystemMessage systemMessage = systemMessageService.querySystemMessageById(id);

        jsonObject.put("systemMessage", systemMessage);

        return jsonObject;
    }

    @ResponseBody
    @PostMapping("/deleteSystemMessage")
    public void deleteSystemMessage(int id) {
        systemMessageService.deleteSystemMessage(id);
    }

    @ResponseBody
    @PostMapping("/deleteSystemMessageList")
    public void deleteSystemMessageList(String idList) {

        String[] id = idList.split(",");

        for (int i = 0; i < id.length; i++) {
            if (!"".equals(id[i])) {
                int idInt = Integer.parseInt(id[i]);
                systemMessageService.deleteSystemMessage(idInt);
            }

        }

    }

    @ResponseBody
    @GetMapping("/searchSystemMessage")
    public JSONObject searchSystemMessage(int pageNo, String searchValue) {

        JSONObject jsonObject = new JSONObject();

        Page<SystemMessage> page = systemMessageService.searchSystemMessageByPageNoAndPageSizeAndSearchValue(pageNo, 10, searchValue);

        jsonObject.put("page", page);

        return jsonObject;
    }

    /**
     * 获取个人系统消息个数
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @GetMapping("/getSystemMassageNum")
    public JSONObject getSystemMassageNum(int userId) {

        JSONObject jsonObject = new JSONObject();

        User user = userSerivce.queryUserByUserId(userId);

        jsonObject.put("messageNum", user.getMessageNum());

        return jsonObject;
    }

}





